<?php
declare (strict_types=1);

namespace app\course\service\courseware\admin;

use app\common\service\BaseService;
use app\course\logic\courseware\admin\Courseware as CoursewareLogic;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

/**
 * Class Courseware
 *
 * @package app\course\service\courseware\admin
 */
class Courseware extends BaseService
{

    /**
     * @Inject()
     *
     * @var CoursewareLogic
     */
    protected $coursewareLogic;

    /**
     * 课件列表
     *
     * @param int $courseId
     * @return \eduline\admin\page\interfaces\think\Response
     * @throws \think\db\exception\DbException
     */
    public function index(int $courseId)
    {
        $map  = [
            'course_id' => $courseId
        ];
        $data = $this->coursewareLogic->getList($map);
        // 定义字段
        $keyList = [
            'attach_title' => ListItem::make()->title('课件名称'),
            'create_time'  => ListItem::make()->title('上传时间')
        ];

        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * 添加课件
     *
     * @param int $courseId
     * @return \think\Response
     */
    public function create(int $courseId)
    {
        $data  = Request::post();
        $count = $this->coursewareLogic->created($courseId, $data);

        return $this->parseToData(['count' => $count], 1, '添加成功');
    }

    /**
     * 移除课件
     *
     * @param int $courseId
     * @param int $coursewareId
     * @return \think\Response
     */
    public function delete(int $courseId, int $coursewareId)
    {
        $this->coursewareLogic->deleted($courseId, $coursewareId);

        return $this->parseToData([], 1, '删除成功');
    }
}